/*
The surface-physics project: http://code.google.com/p/surface-physics/
Adds real world physics based on the Bullet physics engine, to bases in the Orbiter space
flight simulation software.

Written by Abhijit Nandy
License : Apache License 2.0

Bump_CraterRamp.cpp
Bump_CraterRamp implementation.


*/


/* Class declaration */
#include "Bump_CraterRamp.h"


Registrar Bump_CraterRamp::registrar("CraterRamp", Bump_CraterRamp::create);


/**
 * Constructor for this derived class
 */
Bump_CraterRamp::Bump_CraterRamp()
{
	freq = -1; //no updates needed
}


/**
 * Destructor, does nothing currently
 */
Bump_CraterRamp::~Bump_CraterRamp()
{
	//Destroy physics counterpart
	int rv = destroyBulletRigidBody();
	if(rv){
		sprintf(boutput, "Bump_CraterRamp::exit: ERROR : Destroying rigid body failed for %s", strObjectName);
		oapiWriteLog(boutput);
	}

}


/**
 * Initialize a base component.
 * Creates a new BulletBase Attachment and inserts it in the Orbiter world & Bullet World
 *
 *
 */
int
Bump_CraterRamp::init(Bump_BulletBase *bb, char *strObjName,  OBJHANDLE hObj)
{
	int rv;

	sprintf(boutput, "Bump_CraterRamp::init: Initializing %s", strObjName);
	oapiWriteLog(boutput);

	bumpBase = bb;
	strcpy(strObjectName, strObjName);


	//Create Orbiter object: Load any meshes by reading the mesh files from the physics file
	createOrbiterPrimitives();


	//Create physics counterpart, if this fails, should remove Orbiter meshes that were loaded above
	rv = createBulletRigidBody();
	if(rv){
		//Remove here if necess.

		sprintf(boutput, "Bump_CraterRamp::init: ERROR : Creating rigid body failed for %s",
				strObjectName);
		oapiWriteLog(boutput);
		return 1;
	}	

	//Only after Orbiter AND Bullet successful, set physics to true
	physicsInited = true;




	return 0;
}


/**
 *
 *
 */
int
Bump_CraterRamp::createOrbiterPrimitives()
{
	VECTOR3 ofs = _V(50, 0, -200);

	bumpBase->bb->AddMesh("Bump/ComponentLibrary/CraterRamp", &ofs);


	return 0;
}


/**
 * Detach the VESSEL from the BulletBase in the Orbiter world
 *
 */
int
Bump_CraterRamp::destroyOrbiterPrimitives()
{



	return 0;
}


/**
 * Create the physics counterpart in the Bullet world.
 *
 * Preferably get all info required from Orbiter and store in members beforehand.
 *
 * Optimize: reuse box shape by scaling a unit block for the pad sections and blocks
 *
 * TODO: Parse the base cfg file to get the shapes instead of hard coding ? Or merge
 * 		 base cfg with physics file for base ?
 *
 */
int
Bump_CraterRamp::createBulletRigidBody()
{

	btTransform trans;
	btQuaternion q;

	btVector3 ofs(50,0,-200);

	//rbCraterRamp = NULL;

	mshObject.addMSH("Meshes/Bump/ComponentLibrary/CraterRamp.msh", ofs);
	//rbCraterRamp = bump.addOrbiterMshStatic("Meshes/Bump/ComponentLibrary/CraterRamp.msh", ofs);


/*	if(rbCraterRamp){
		sprintf(boutput, "Bump_CraterRamp::createBulletRigidBody: Created rigid body for %s",
				strObjectName);
		oapiWriteLog(boutput);
	}
	else{
		sprintf(boutput, "Bump_CraterRamp::createBulletRigidBody: ERROR Could not create rigid body for %s",
						strObjectName);
		oapiWriteLog(boutput);
	}
*/

	return 0;
}


/**
 * De-allocates memory being consumed by the raycast vehicle so it can be
 * created again by createBulletRigidBody()
 *
 * This is a very important function. If memory is not freed up, Orbiter sim will not
 * exit properly and the Launchpad will not appear leading to user frustration!
 *
 * Here we de-allocate only the sub class specific stuff which have not been freed by
 * bump.clientResetScene(), which is called before Bump_BulletBase::exit();
 *
 * Thus rigid bodies(derived from CollisionObjects), motion states are already removed
 * by the time we come here, DO NOT remove them again !
 *
 * The stuff which should be removed are any sub class specific classes used to model
 * this object like m_vehicleRayCaster & m_vehicle. Also any collision shapes not pushed
 * into m_collisionShapes[] during init(), needs to be freed here, such as the wheelShape
 */
int
Bump_CraterRamp::destroyBulletRigidBody()
{
	physicsInited = false;



	return 0;
}
